package com.xiaozhichao.databafflesys.common.utils;

import cn.hutool.core.util.XmlUtil;
import com.alibaba.fastjson.JSON;
import com.xiaozhichao.databafflesys.common.domain.CommonConstant;
import org.custommonkey.xmlunit.Diff;
import org.w3c.dom.Document;

/**
 * @author:xiaozhichao
 * @date:2021/3/22
 * 报文比较
 */
public class ReqCompare {

    public static boolean compare(String type,String target,String source){
        if(CommonConstant.json.equals(type)){
            return compareJSON(target,source);
        }else if(CommonConstant.xml.equals(type)){
            return compareXml(target,source);
        }else if(CommonConstant.queryString.equals(type)){
            return compareJSON(target,source);
        }else if(CommonConstant.formData.equals(type)){
            return compareJSON(target,source);
        }
        return false;
    }

    public static boolean compareJSON(String target,String source){
        Object parseTarget = JSON.parse(target);
        Object parseSource = JSON.parse(source);
        return parseTarget.equals(parseSource);
    }

    public static boolean compareXml(String target,String source){
        Document document = XmlUtil.parseXml(target);
        Document document1 = XmlUtil.parseXml(source);
        Diff diff = new Diff(document,document1);
        return diff.similar();
    }

    public static boolean compareQueryString(String target,String source){
        return target.equals(source);
    }

    public static boolean compareFormData(String target,String source){
        return target.equals(source);
    }

}
